package client.view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.util.ArrayList;
import java.util.List;

import sharedObjects.Field;
import client.controler.LaunchController;

/**
 * 
 * @author Niclas
 */
public class Drawer {

	private static Drawer instance = null;

	public static Drawer getInstance() {
		if (instance == null) {
			instance = new Drawer();
		}
		return instance;
	}

	public Drawer() {
		// TODO Auto-generated constructor stub
	}

	static int MARGIN_LEFT = 10;
	static int MARGIN_TOP = 10;
	static int FIELD_HEIGHT = 40;
	static int FIELD_WIDTH = 40;
	static int FIELD_COUNT_HORIZONTAL = 10;
	static int Field_COUNT_VETICAL = 10;
	static int MARGIN_BETWEEN_FIELDGROUPS = 420;

	public void drawField(Graphics g) {

		// Draw Background Field 1
		g.setColor(new Color(135, 206, 255));

		g.fillRect(MARGIN_LEFT, MARGIN_TOP, FIELD_WIDTH
				* FIELD_COUNT_HORIZONTAL, FIELD_HEIGHT * Field_COUNT_VETICAL);

		// Draw Grid Field 1
		g.setColor(Color.DARK_GRAY);
		LaunchController lc = LaunchController.getInstance();
		for (int x = MARGIN_LEFT; x < FIELD_COUNT_HORIZONTAL * FIELD_WIDTH; x = x
				+ FIELD_WIDTH) {
			for (int y = MARGIN_TOP; y < Field_COUNT_VETICAL * FIELD_HEIGHT; y = y
					+ FIELD_HEIGHT) {
				g.drawRect(x, y, FIELD_WIDTH, FIELD_HEIGHT);

				lc.fieldStorage.fieldList.add(new Field(
						lc.fieldStorage.fieldList.size() + 1, new Point(x, y),
						new Point(x + FIELD_WIDTH, y + FIELD_HEIGHT),
						new Point(x / FIELD_WIDTH, y / FIELD_HEIGHT), false));

			}
		}

		// Draw Background Field 2
		g.setColor(new Color(135, 206, 255));
		g.fillRect(MARGIN_LEFT + MARGIN_BETWEEN_FIELDGROUPS, MARGIN_TOP,
				FIELD_WIDTH * FIELD_COUNT_HORIZONTAL, FIELD_HEIGHT
						* Field_COUNT_VETICAL);

		// Draw Grid Field 2
		g.setColor(Color.DARK_GRAY);
		for (int x = MARGIN_LEFT + MARGIN_BETWEEN_FIELDGROUPS; x < (FIELD_COUNT_HORIZONTAL * FIELD_WIDTH)
				+ MARGIN_BETWEEN_FIELDGROUPS; x = x + FIELD_WIDTH) {
			for (int y = MARGIN_TOP; y < Field_COUNT_VETICAL * FIELD_HEIGHT; y = y
					+ FIELD_HEIGHT) {
				g.drawRect(x, y, FIELD_WIDTH, FIELD_HEIGHT);
				lc.fieldStorageEnemy.fieldList.add(new Field(
						lc.fieldStorage.fieldList.size() + 1, new Point(x, y),
						new Point(x + FIELD_WIDTH, y + FIELD_HEIGHT),
						new Point(x / FIELD_WIDTH, y / FIELD_HEIGHT), false));
			}
		}

	}

	public void drawHits(Graphics g) {
		LaunchController lc = LaunchController.getInstance();
		for (int i = 0; i < lc.fieldStorageEnemy.fieldList.size(); i++) {
			if (lc.fieldStorageEnemy.fieldList.get(i).isHitten()) {
				g.setColor(Color.RED);
				g.fillOval(
						lc.fieldStorageEnemy.fieldList.get(i).getUpperLeftPixels().x,
						lc.fieldStorageEnemy.fieldList.get(i).getUpperLeftPixels().y,
						FIELD_WIDTH, FIELD_HEIGHT);
			}
		}

	}

	public void drawShip(Graphics g) {
		g.setColor(Color.DARK_GRAY);
		LaunchController lc = LaunchController.getInstance();
		for (int i = 0; i < lc.shipStorage.ships.size(); i++) {
			for (int j = 0; j < lc.shipStorage.ships.get(i).shipfieldList
					.size(); j++) {
				Point ul = lc.shipStorage.ships.get(i).shipfieldList.get(j)
						.getUpperLeftPixels();
				g.fillOval(ul.x, ul.y, FIELD_WIDTH, FIELD_HEIGHT);
			}

		}

	}

}
